package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbActive;
import com.bwie.service.TbActiveService;
import com.bwie.mapper.TbActiveMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
* @author 叶雨洁
* @description 针对表【tb_active(抽奖活动表)】的数据库操作Service实现
* @createDate 2025-07-28 20:49:48
*/
@Service
public class TbActiveServiceImpl extends ServiceImpl<TbActiveMapper, TbActive>
    implements TbActiveService{

    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Autowired
    TbActiveMapper tbActiveMapper;

    @Override
    public R uploadImage(MultipartFile file) {

        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);

            String url="http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(url);
        } catch (IOException e) {
            e.printStackTrace();
            return R.ERROR(500,"上传失败");
        }
    }

    @Override
    public R TeamPageList(PageInfoVo pageInfoVo) {

        Page<TbActive> tbActivePage = new Page<>(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());
        QueryWrapper<TbActive> queryWrapper = new QueryWrapper<>();

        Page<TbActive> tbActivePage1 = tbActiveMapper.selectPage(tbActivePage, queryWrapper);
        return R.OK(tbActivePage1);
    }

    @Override
    public R insert(TbActive tbActive) {

        TbActive tbActive1 = tbActiveMapper.selectOne(
                new QueryWrapper<TbActive>().lambda().eq(TbActive::getActiveId, tbActive.getActiveId())
        );
        if(tbActive1!=null){
            return R.ERROR(500,"活动已存在");
        }

        tbActiveMapper.insert(tbActive);
        return R.OK();
    }

    @Override
    public R updateActive(TbActive tbActive) {
        TbActive tbActive1 = tbActiveMapper.selectOne(
                new QueryWrapper<TbActive>().lambda().eq(TbActive::getActiveId, tbActive.getActiveId())
        );
       if(tbActive1==null){
           return R.ERROR(500,"活动不存在");
       }


        return R.OK();
    }

    @Override
    public R delete(Integer activeId) {

        tbActiveMapper.deleteById(activeId);
        return R.OK();
    }

    @Override
    public R getRuleList(Integer activeId) {

        List<TbActive> tbActiveList = tbActiveMapper.selectList(
                new QueryWrapper<TbActive>().lambda().eq(TbActive::getActiveId, activeId)
        );
        return R.OK(tbActiveList);
    }


}




